package com.macro.mall.portal.controller;

import com.macro.mall.common.api.CommonResult;
import com.macro.mall.portal.domain.MemberVO;
import com.macro.mall.portal.service.UmsMemberService;
import com.macro.mall.portal.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author gao
 * @version 1.0
 * @date 2021/9/10
 */
@Slf4j
@RestController
@Api(tags = "WeChatController", description = "微信登录Controller")
@RequestMapping("/oauth")
public class RestAuthController {

//    @Autowired
//    private AuthStateRedisCache stateRedisCache;
    @Autowired
    private UserService userService;
    @Autowired
    private UmsMemberService memberService;
    @Value("${wechat.callback}")
    private String callback;




    /**
     * @throws
     * @title weChatLogin
     * @description 微信授权登录
     * @author Kuangzc
     * @updateTime 2019-9-12 16:00:51
     */
    @ApiOperation(value = "微信授权登录")
    @GetMapping("/weChatLogin")
    public CommonResult weChatLogin(@ApiParam(value = "token") @RequestParam String token, @ApiParam(value = "openId")@RequestParam String openId) {
        Map<String, Object> stringObjectMap = memberService.weChatLogin(token, openId);
        return CommonResult.success(stringObjectMap);
    }

    @ApiOperation("微信绑定会员")
    @RequestMapping(value = "/bindWeChat", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult bindWeChat(@ApiParam(value = "请求报文") @RequestBody MemberVO request) {
        Map<String, Object> register = memberService.bindWeChat(request);
        return CommonResult.success(register);
    }




//
//    @ResponseBody
//    @RequestMapping(value = "/render",method = RequestMethod.POST)
//    @ApiOperation("微信登录")
//    public void renderAuth(@ApiParam(value = "第三方登录枚举   : wechat_open ") @RequestParam(value = "source", required = true) String source,
//                           HttpServletResponse response) throws IOException {
//        log.info("进入render：" + source);
//        AuthRequest authRequest = getAuthRequest(source);
//        String authorizeUrl = authRequest.authorize(AuthStateUtils.createState());
//        log.info(authorizeUrl);
//        response.sendRedirect(authorizeUrl);
//    }
//
//    /**
//     * oauth平台中配置的授权回调地址，以本项目为例，在创建github授权应用时的回调地址应为：http://127.0.0.1:8443/oauth/callback/github
//     */
//    @RequestMapping(value = "/callback/{source}",method = RequestMethod.POST)
//    public CommonResult login(@PathVariable("source") String source, AuthCallback callback, HttpServletRequest request) {
//        log.info("进入callback：" + source + " callback params：" + JSONObject.toJSONString(callback));
//        AuthRequest authRequest = getAuthRequest(source);
//        AuthResponse<AuthUser> response = authRequest.login(callback);
//        log.info(JSONObject.toJSONString(response));
//
//        if (response.ok()) {
////            AuthUser data = response.getData();
////            MemberVO vo = new MemberVO();
////            vo.setUsername(data.getUsername());
////            vo.setName(data.getNickname());
////            //获取用户手机号码替换uuid   手机号后添加 +":wechat"
//            userService.save(response.getData());
//
////            Map<String, Object> register = memberService.register(vo);
//            return CommonResult.success(response.getData());
//        }
//
//        Map<String, Object> map = new HashMap<>(1);
//        map.put("errorMsg", response.getMsg());
//
//        return CommonResult.unauthorized(null);
//    }
//
//
////    @ResponseBody
////    @RequestMapping(value="/revoke/{source}/{uuid}",method = RequestMethod.POST)
////    public Response revokeAuth(@ApiParam(value = "第三方登录枚举   : wechat_open ") @RequestParam(value = "source", required = true) String source,
////                               @ApiParam(value = "用户手机号码") @RequestParam(value = "mobile", required = true) String mobile) throws IOException {
////        AuthRequest authRequest = getAuthRequest(source.toLowerCase());
////
////        AuthUser user = userService.getByUuid(mobile+":wechat");
//////        UmsMember user = memberService.getByUsername(mobile);
////        if (null == user) {
////            return Response.error("用户不存在");
////        }
////        AuthResponse<AuthToken> response = null;
////        try {
////            response = authRequest.revoke(user.getToken());
////            if (response.ok()) {
////                userService.remove(user.getUuid());
////                return Response.success("用户 [" + user.getUsername() + "] 的 授权状态 已收回！");
////            }
////            return Response.error("用户 [" + user.getUsername() + "] 的 授权状态 收回失败！" + response.getMsg());
////        } catch (AuthException e) {
////            return Response.error(e.getErrorMsg());
////        }
////    }
////
////    @RequestMapping(value="/refresh/{source}/{uuid}",method = RequestMethod.POST)
////    @ResponseBody
////    public Object refreshAuth(@PathVariable("source") String source, @PathVariable("uuid") String uuid) {
////        AuthRequest authRequest = getAuthRequest(source.toLowerCase());
////
////        AuthUser user = userService.getByUuid(uuid);
////        if (null == user) {
////            return Response.error("用户不存在");
////        }
////        AuthResponse<AuthToken> response = null;
////        try {
////            response = authRequest.refresh(user.getToken());
////            if (response.ok()) {
////                user.setToken(response.getData());
////                userService.save(user);
////                return Response.success("用户 [" + user.getUsername() + "] 的 access token 已刷新！新的 accessToken: " + response.getData().getAccessToken());
////            }
////            return Response.error("用户 [" + user.getUsername() + "] 的 access token 刷新失败！" + response.getMsg());
////        } catch (AuthException e) {
////            return Response.error(e.getErrorMsg());
////        }
////    }
//
//    /**
//     * 根据具体的授权来源，获取授权请求工具类
//     *
//     * @param source
//     * @return
//     */
//    private AuthRequest getAuthRequest(String source) {
//        AuthRequest authRequest = null;
//        switch (source.toLowerCase()) {
//            case "dingtalk":
//                authRequest = new AuthDingTalkRequest(AuthConfig.builder()
//                        .clientId("")
//                        .clientSecret("")
//                        .redirectUri("http://localhost:8443/oauth/callback/dingtalk")
//                        .build());
//                break;
//            case "baidu":
//                authRequest = new AuthBaiduRequest(AuthConfig.builder()
//                        .clientId("")
//                        .clientSecret("")
//                        .redirectUri("http://localhost:8443/oauth/callback/baidu")
//                        .scopes(Arrays.asList(
//                                AuthBaiduScope.BASIC.getScope(),
//                                AuthBaiduScope.SUPER_MSG.getScope(),
//                                AuthBaiduScope.NETDISK.getScope()
//                        ))
////                        .clientId("")
////                        .clientSecret("")
////                        .redirectUri("http://localhost:9001/oauth/baidu/callback")
//                        .build());
//                break;
////            case "github":
////                authRequest = new AuthGithubRequest(AuthConfig.builder()
////                        .clientId("")
////                        .clientSecret("")
////                        .redirectUri("http://localhost:8443/oauth/callback/github")
////                        .scopes(AuthScopeUtils.getScopes(AuthGithubScope.values()))
////                        // 针对国外平台配置代理
////                        .httpConfig(HttpConfig.builder()
////                                .timeout(15000)
////                                .proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 10080)))
////                                .build())
////                        .build(), stateRedisCache);
////                break;
////            case "gitee":
////                authRequest = new AuthGiteeRequest(AuthConfig.builder()
////                        .clientId("")
////                        .clientSecret("")
////                        .redirectUri("http://127.0.0.1:8443/oauth/callback/gitee")
////                        .scopes(AuthScopeUtils.getScopes(AuthGiteeScope.values()))
////                        .build(), stateRedisCache);
////                break;
//            case "weibo":
//                authRequest = new AuthWeiboRequest(AuthConfig.builder()
//                        .clientId("")
//                        .clientSecret("")
//                        .redirectUri("http://dblog-web.zhyd.me/oauth/callback/weibo")
//                        .scopes(Arrays.asList(
//                                AuthWeiboScope.EMAIL.getScope(),
//                                AuthWeiboScope.FRIENDSHIPS_GROUPS_READ.getScope(),
//                                AuthWeiboScope.STATUSES_TO_ME_READ.getScope()
//                        ))
//                        .build());
//                break;
//            case "coding":
//                authRequest = new AuthCodingRequest(AuthConfig.builder()
//                        .clientId("")
//                        .clientSecret("")
//                        .redirectUri("http://dblog-web.zhyd.me/oauth/callback/coding")
//                        .codingGroupName("")
//                        .scopes(Arrays.asList(
//                                AuthCodingScope.USER.getScope(),
//                                AuthCodingScope.USER_EMAIL.getScope(),
//                                AuthCodingScope.USER_PHONE.getScope()
//                        ))
//                        .build());
//                break;
//            case "oschina":
//                authRequest = new AuthOschinaRequest(AuthConfig.builder()
//                        .clientId("")
//                        .clientSecret("")
//                        .redirectUri("http://localhost:8443/oauth/callback/oschina")
//                        .build());
//                break;
//            case "alipay":
//                // 支付宝在创建回调地址时，不允许使用localhost或者127.0.0.1，所以这儿的回调地址使用的局域网内的ip
//                authRequest = new AuthAlipayRequest(AuthConfig.builder()
//                        .clientId("")
//                        .clientSecret("")
//                        .alipayPublicKey("")
//                        .redirectUri("https://www.zhyd.me/oauth/callback/alipay")
//                        .build());
//                break;
//            case "qq":
//                authRequest = new AuthQqRequest(AuthConfig.builder()
//                        .clientId("")
//                        .clientSecret("")
//                        .redirectUri("http://localhost:8443/oauth/callback/qq")
//                        .build());
//                break;
//            case "wechat_open":
//                authRequest = new AuthWeChatOpenRequest(AuthConfig.builder()
//                        .clientId("wx0f11e6b191352201")
//                        .clientSecret("0c5abfb095992ad996003991c0b928dc")
//                        .redirectUri(callback)
//                        .build());
//                break;
//            case "csdn":
//                authRequest = new AuthCsdnRequest(AuthConfig.builder()
//                        .clientId("")
//                        .clientSecret("")
//                        .redirectUri("http://dblog-web.zhyd.me/oauth/callback/csdn")
//                        .build());
//                break;
//            case "taobao":
//                authRequest = new AuthTaobaoRequest(AuthConfig.builder()
//                        .clientId("")
//                        .clientSecret("")
//                        .redirectUri("http://dblog-web.zhyd.me/oauth/callback/taobao")
//                        .build());
//                break;
//            case "google":
//                authRequest = new AuthGoogleRequest(AuthConfig.builder()
//                        .clientId("")
//                        .clientSecret("")
//                        .redirectUri("http://localhost:8443/oauth/callback/google")
//                        .scopes(AuthScopeUtils.getScopes(AuthGoogleScope.USER_EMAIL, AuthGoogleScope.USER_PROFILE, AuthGoogleScope.USER_OPENID))
//                        // 针对国外平台配置代理
//                        .httpConfig(HttpConfig.builder()
//                                .timeout(15000)
//                                .proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 10080)))
//                                .build())
//                        .build());
//                break;
//            case "facebook":
//                authRequest = new AuthFacebookRequest(AuthConfig.builder()
//                        .clientId("")
//                        .clientSecret("")
//                        .redirectUri("https://justauth.cn/oauth/callback/facebook")
//                        .scopes(AuthScopeUtils.getScopes(AuthFacebookScope.values()))
//                        // 针对国外平台配置代理
//                        .httpConfig(HttpConfig.builder()
//                                .timeout(15000)
//                                .proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 10080)))
//                                .build())
//                        .build());
//                break;
//            case "douyin":
//                authRequest = new AuthDouyinRequest(AuthConfig.builder()
//                        .clientId("")
//                        .clientSecret("")
//                        .redirectUri("http://dblog-web.zhyd.me/oauth/callback/douyin")
//                        .build());
//                break;
//            case "linkedin":
//                authRequest = new AuthLinkedinRequest(AuthConfig.builder()
//                        .clientId("")
//                        .clientSecret("")
//                        .redirectUri("http://localhost:8443/oauth/callback/linkedin")
//                        .scopes(null)
//                        .build());
//                break;
//            case "microsoft":
//                authRequest = new AuthMicrosoftRequest(AuthConfig.builder()
//                        .clientId("")
//                        .clientSecret("")
//                        .redirectUri("http://localhost:8443/oauth/callback/microsoft")
//                        .scopes(Arrays.asList(
//                                AuthMicrosoftScope.USER_READ.getScope(),
//                                AuthMicrosoftScope.USER_READWRITE.getScope(),
//                                AuthMicrosoftScope.USER_READBASIC_ALL.getScope(),
//                                AuthMicrosoftScope.USER_READ_ALL.getScope(),
//                                AuthMicrosoftScope.USER_READWRITE_ALL.getScope(),
//                                AuthMicrosoftScope.USER_INVITE_ALL.getScope(),
//                                AuthMicrosoftScope.USER_EXPORT_ALL.getScope(),
//                                AuthMicrosoftScope.USER_MANAGEIDENTITIES_ALL.getScope(),
//                                AuthMicrosoftScope.FILES_READ.getScope()
//                        ))
//                        .build());
//                break;
//            case "mi":
//                authRequest = new AuthMiRequest(AuthConfig.builder()
//                        .clientId("")
//                        .clientSecret("")
//                        .redirectUri("http://dblog-web.zhyd.me/oauth/callback/mi")
//                        .build());
//                break;
//            case "toutiao":
//                authRequest = new AuthToutiaoRequest(AuthConfig.builder()
//                        .clientId("")
//                        .clientSecret("")
//                        .redirectUri("http://dblog-web.zhyd.me/oauth/callback/toutiao")
//                        .build());
//                break;
//            case "teambition":
//                authRequest = new AuthTeambitionRequest(AuthConfig.builder()
//                        .clientId("")
//                        .clientSecret("")
//                        .redirectUri("http://127.0.0.1:8443/oauth/callback/teambition")
//                        .build());
//                break;
//            case "pinterest":
//                authRequest = new AuthPinterestRequest(AuthConfig.builder()
//                        .clientId("")
//                        .clientSecret("")
//                        .redirectUri("https://eadmin.innodev.com.cn/oauth/callback/pinterest")
//                        // 针对国外平台配置代理
//                        .httpConfig(HttpConfig.builder()
//                                .timeout(15000)
//                                .proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 10080)))
//                                .build())
//                        .build());
//                break;
//            case "renren":
//                authRequest = new AuthRenrenRequest(AuthConfig.builder()
//                        .clientId("")
//                        .clientSecret("")
//                        .redirectUri("http://127.0.0.1:8443/oauth/callback/teambition")
//                        .build());
//                break;
//            case "stack_overflow":
//                authRequest = new AuthStackOverflowRequest(AuthConfig.builder()
//                        .clientId("")
//                        .clientSecret("((")
//                        .redirectUri("http://localhost:8443/oauth/callback/stack_overflow")
//                        .stackOverflowKey("")
//                        .build());
//                break;
//            case "huawei":
//                authRequest = new AuthHuaweiRequest(AuthConfig.builder()
//                        .clientId("")
//                        .clientSecret("")
//                        .redirectUri("http://127.0.0.1:8443/oauth/callback/huawei")
//                        .scopes(Arrays.asList(
//                                AuthHuaweiScope.BASE_PROFILE.getScope(),
//                                AuthHuaweiScope.MOBILE_NUMBER.getScope(),
//                                AuthHuaweiScope.ACCOUNTLIST.getScope(),
//                                AuthHuaweiScope.SCOPE_DRIVE_FILE.getScope(),
//                                AuthHuaweiScope.SCOPE_DRIVE_APPDATA.getScope()
//                        ))
//                        .build());
//                break;
//            case "wechat_enterprise":
//                authRequest = new AuthWeChatEnterpriseQrcodeRequest(AuthConfig.builder()
//                        .clientId("")
//                        .clientSecret("")
//                        .redirectUri("http://justauth.cn/oauth/callback/wechat_enterprise")
//                        .agentId("1000003")
//                        .build());
//                break;
//            case "kujiale":
//                authRequest = new AuthKujialeRequest(AuthConfig.builder()
//                        .clientId("")
//                        .clientSecret("")
//                        .redirectUri("http://dblog-web.zhyd.me/oauth/callback/kujiale")
//                        .build());
//                break;
//            case "gitlab":
//                authRequest = new AuthGitlabRequest(AuthConfig.builder()
//                        .clientId("")
//                        .clientSecret("")
//                        .redirectUri("http://localhost:8443/oauth/callback/gitlab")
//                        .scopes(AuthScopeUtils.getScopes(AuthGitlabScope.values()))
//                        .build());
//                break;
//            case "meituan":
//                authRequest = new AuthMeituanRequest(AuthConfig.builder()
//                        .clientId("")
//                        .clientSecret("")
//                        .redirectUri("http://localhost:8443/oauth/callback/meituan")
//                        .build());
//                break;
//            case "eleme":
//                authRequest = new AuthElemeRequest(AuthConfig.builder()
//                        .clientId("")
//                        .clientSecret("")
//                        .redirectUri("http://dblog-web.zhyd.me/oauth/callback/eleme")
//                        .build());
//                break;
//            case "mygitlab":
////                authRequest = new AuthMyGitlabRequest(AuthConfig.builder()
////                        .clientId("")
////                        .clientSecret("")
////                        .redirectUri("http://127.0.0.1:8443/oauth/callback/mygitlab")
////                        .build());
////                break;
//            case "twitter":
//                authRequest = new AuthTwitterRequest(AuthConfig.builder()
//                        .clientId("")
//                        .clientSecret("")
//                        .redirectUri("https://threelogin.31huiyi.com/oauth/callback/twitter")
//                        // 针对国外平台配置代理
//                        .httpConfig(HttpConfig.builder()
//                                .timeout(15000)
//                                .proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 10080)))
//                                .build())
//                        .build());
//                break;
//            case "wechat_mp":
//                authRequest = new AuthWeChatMpRequest(AuthConfig.builder()
//                        .clientId("")
//                        .clientSecret("")
//                        .redirectUri("")
//                        .build());
//                break;
//            case "aliyun":
//                authRequest = new AuthAliyunRequest(AuthConfig.builder()
//                        .clientId("")
//                        .clientSecret("")
//                        .redirectUri("http://localhost:8443/oauth/callback/aliyun")
//                        .build());
//                break;
//            case "xmly":
//                authRequest = new AuthXmlyRequest(AuthConfig.builder()
//                        .clientId("")
//                        .clientSecret("")
//                        .redirectUri("http://localhost:8443/oauth/callback/xmly")
//                        .build());
//                break;
//            case "feishu":
//                authRequest = new AuthFeishuRequest(AuthConfig.builder()
//                        .clientId("")
//                        .clientSecret("")
//                        .redirectUri("http://localhost:8443/oauth/callback/feishu")
//                        .build());
//                break;
//            default:
//                break;
//        }
//        if (null == authRequest) {
//            throw new AuthException("未获取到有效的Auth配置");
//        }
//        return authRequest;
//    }
}
